﻿using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{
	[HideInInspector]
	public static PlayerController instance; // static allows access from anywhere

	public float speed;
	public float waitTime;
	public bool moving;

	[HideInInspector]
	public float timeTest;
	
	private Transform myTransform;
	public Vector3 checkPoint;
	public float testZ;

	private float waiterTime;
	private Vector3 nPos = new Vector3();

	void Awake()
	{
		instance = this;
	}

	void Start()
	{
		waiterTime = 0;

		myTransform = transform;

		checkPoint = new Vector3 (0.0f, 0.0f, 0.0f);

		moving = false;
	}
	
	void Update()
	{
		if (!moving)
		{
			return;
		}

		waiterTime -= Time.deltaTime;
		//waiterTime -= Time.fixedDeltaTime;

		if (waiterTime > 0)
		{
			Time.timeScale = 0.1f;
		}
		else
		{
			Time.timeScale = 1.0f;
		}

		if (Input.anyKeyDown) // This works, it captures the press and then which was pressed
			ButtonPress();
			
		myTransform.position += Vector3.forward * speed * Time.deltaTime;
		//myTransform.position += Vector3.forward * speed * Time.fixedDeltaTime;

		nPos.z = myTransform.position.z;

		myTransform.position = Vector3.MoveTowards (myTransform.position, nPos, speed * Time.deltaTime);
		//myTransform.position = Vector3.MoveTowards (myTransform.position, nPos, speed * Time.fixedDeltaTime);

//		RaycastHit hit;
//		if (Physics.Raycast(myTransform.position, Vector3.forward, out hit, 2.0f))
//		{
//			if (hit.collider.CompareTag("Wall"))
//				Respawn();
//			
//			else if (hit.collider.CompareTag("CheckPoint"))
//			{
//				testZ = hit.transform.parent.position.z;
//				checkPoint = new Vector3 (0.0f, 0.0f, testZ);
//			}
//			else if (hit.collider.CompareTag("Finish")) // Tags made in unity //compare tag is faster, makes it a number instead of string
//			{
//				timeTest = HUDManager.instance.GetTime();
//				Application.LoadLevel("Level_Menu"); // Load up the level menu
//			}
//		}
	}

	void OnTriggerEnter (Collider other)
	{
		if (other.CompareTag("Wall"))
			Respawn();

		else if (other.CompareTag("CheckPoint"))
		{
			testZ = other.transform.parent.position.z;
			checkPoint = new Vector3 (0.0f, 0.0f, testZ);
		}
		else if (other.CompareTag("Finish")) // Tags made in unity //compare tag is faster, makes it a number instead of string
		{
			timeTest = HUDManager.instance.GetTime();
			Application.LoadLevel("Level_Menu"); // Load up the level menu
		}
	}

	private void Respawn ()
	{
		testZ = checkPoint.z;
		myTransform.position = new Vector3 (0.0f, 0.0f, testZ);
		SetNPos(new Vector3(0.0f, 0.0f, testZ)); // Stops the player from wanting to move after wall hit while moving
		FXManager.instance.SpawnFX("Respawn", transform.position, transform.rotation);
		waiterTime = waitTime;
	}

	void ButtonPress()
	{
		if (Input.GetKeyDown(KeyCode.Keypad5))
			ChangeLane(4);

		else if (Input.GetKeyDown(KeyCode.Keypad1))
			ChangeLane(0);
		
		else if (Input.GetKeyDown(KeyCode.Keypad2))
			ChangeLane(1);
		
		else if (Input.GetKeyDown(KeyCode.Keypad3))
			ChangeLane(2);
		
		else if (Input.GetKeyDown(KeyCode.Keypad4))
			ChangeLane(3);
		
		else if (Input.GetKeyDown(KeyCode.Keypad6))
			ChangeLane(5);
		
		else if (Input.GetKeyDown(KeyCode.Keypad7))
			ChangeLane(6);
		
		else if (Input.GetKeyDown(KeyCode.Keypad8))
			ChangeLane(7);
		
		else if (Input.GetKeyDown(KeyCode.Keypad9))
			ChangeLane(8);
	}

	void ChangeLane(int key)
	{
		Vector3 lPos = LaneManager.instance.MoveTo (key); // Get new X and Z

		nPos = new Vector3(lPos.x, lPos.y, myTransform.position.z); // Change new location's X and Y, but keeping the same Z
	}

	void SetNPos (Vector3 setTo)
	{
		nPos = setTo;
	}

}